package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.appsdks.PushGetuiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.system.mapper.SysAppPushMapper;
import com.ruoyi.system.domain.SysAppPush;
import com.ruoyi.system.service.ISysAppPushService;

import java.util.Date;
import java.util.List;

/**
 * APP推送管理 Service业务层处理
 *
 * @author shuyan
 * @date 2021-12-07
 */
@Service
public class SysAppPushServiceImpl extends ServiceImpl<SysAppPushMapper, SysAppPush> implements ISysAppPushService {

    /**
     * 查询APP推送管理
     *
     * @param sysAppPush 查询参数
     * @return 结果列表
     */
    @Override
    public List<SysAppPush> queryList(SysAppPush sysAppPush) {
        LambdaQueryWrapper<SysAppPush> lqw = Wrappers.lambdaQuery();

        if (StringUtils.isNotEmpty(sysAppPush.getClientId())) {
            lqw.eq(SysAppPush::getClientId, sysAppPush.getClientId());
        }
        if (StringUtils.isNotEmpty(sysAppPush.getAppToken())) {
            lqw.eq(SysAppPush::getAppToken, sysAppPush.getAppToken());
        }
        if (StringUtils.isNotEmpty(sysAppPush.getPushType())) {
            lqw.eq(SysAppPush::getPushType, sysAppPush.getPushType());
        }
        if (StringUtils.isNotEmpty(sysAppPush.getUserName())) {
            lqw.eq(SysAppPush::getUserName, sysAppPush.getUserName());
        }

        return this.list(lqw);
    }


    /**
     * 数据导入方法
     *
     * @param excelList     excel中读取的数据
     * @param updateSupport 是否覆盖更新
     * @param extraParams   额外参数
     */
    @Override
    public String importData(List<SysAppPush> excelList, boolean updateSupport, String extraParams) {
        //TODO  此处填写导入方法实现 如果默认直接添加,使用以下方法: 
        //this.saveBatch(excelList);
        return "操作成功";
    }

    @Override
    public void bindClient(SysAppPush client) {
        String username = SecurityUtils.getUsername();
        client.setUserName(username);

        SysAppPush old = getById(client.getClientId());
        boolean needBind = false;
        if (old != null) {
            client.setUpdateTime(new Date());
            needBind = !old.getUserName().equals(client.getUserName());
        } else {
            client.setCreateTime(new Date());
            needBind = true;
        }
        saveOrUpdate(client);
        //添加或更新标签
        if (needBind) {
            PushGetuiUtils.bindUser(client.getClientId(), client.getUserName());
        }
    }
}
